<?php

/******************************************
 * File:			lib_err.php
 * Version:			1.0.1
 * Last modified:	25/10/2009
 * Last modifier:	Hugo Lavigne
 *****************************************/
 
 #Load logs configuration
	 GLOBAL $path_config_logs;
	 GLOBAL $path_logs;
	 DEFINE (LOGS_CONF, $path_config_logs);
	 DEFINE (LOGS_PATH, $path_logs);
 
Class PW_err
{	
	public	$epage;
	public	$eLine;
	
	private	$eSysCount;
	private	$eUsrCount;
	private	$enable_sys_logs;
	private	$visible_sys_logs;
	private	$sys_logs_name;
	private	$sys_logs_size;
	private	$enable_usr_logs;
	private	$visible_usr_logs;
	private	$usr_logs_name;
	private	$usr_logs_size;
	private	$size;
	private	$type;
	private	$path;
	
	public
		Function __construct() 
		{
			require_once (LOGS_CONF);
			$this->enable_sys_logs = $enable_sys_logs;
			$this->visible_sys_logs = $visible_sys_logs;
			$this->sys_logs_name = $sys_logs_name;
			$this->sys_logs_size = $sys_logs_size;
			
			$this->enable_usr_logs = $enable_usr_logs;
			$this->visible_usr_logs = $visible_usr_logs;
			$this->usr_logs_name = $usr_logs_name;
			$this->usr_logs_size = $usr_logs_size;
			
			$this->setSize();
			$this->eSysCount = 0;
			$this->eUsrCount = 0;
			$this->ePage = 0;
			$this->eLine = 0;
		}
		Function __destruct() {}
		
		Function setSize($size='-')
		{
			$this->size = $size;
			return;
		}
		
		Function sNotice($msg, $noticeNo=0)
		{
			if ($this->visible_sys_logs)
			{
				if ($this->size == '-')
					$this->setSize($this->sys_logs_size);
				$this->showError($msg, 'notice', $noticeNo);
			}
			if ($this->enable_sys_logs)
			{
				$this->path = LOGS_PATH.$this->sys_logs_name.'.txt';
				$this->type = 'sysN';
				$this->addLogs($msg, 'Notice', $noticeNo);
			}
			return;
		}
		
		Function sHelp($msg, $helpNo=0)
		{
			if ($this->visible_sys_logs)
			{
				if ($this->size == '-')
					$this->setSize($this->sys_logs_size);
				$this->showError($msg, 'help', $helpNo);
			}
			if ($this->enable_sys_logs)
			{
				$this->path = LOGS_PATH.$this->sys_logs_name.'.txt';
				$this->type = 'sysH';
				$this->addLogs($msg, 'Help', $helpNo);
			}
			return;
		}
		
		Function sInfos($msg, $infoNo=0)
		{
			if ($this->visible_sys_logs)
			{
				if ($this->size == '-')
					$this->setSize($this->sys_logs_size);
				$this->showError($msg, 'infos', $infoNo);
			}
			if ($this->enable_sys_logs)
			{
				$this->path = LOGS_PATH.$this->sys_logs_name.'.txt';
				$this->type = 'sysI';
				$this->addLogs($msg, 'Infos', $infoNo);
			}
			return;
		}
		
		Function sWarning($msg, $errNo=0)
		{
			if ($this->visible_sys_logs)
			{
				if ($this->size == '-')
					$this->setSize($this->sys_logs_size);
				$this->showError($msg, 'warning', $errNo);
			}
			if ($this->enable_sys_logs)
			{
				$this->path = LOGS_PATH.$this->sys_logs_name.'.txt';
				$this->type = 'sysE';
				$this->addLogs($msg, 'Warning', $errNo);
			}
			$this->eSysCount++;
			return;
		}
		
		Function notice($msg, $noticeNo=0)
		{
			if ($this->visible_usr_logs)
			{
				if ($this->size == '-')
					$this->setSize($this->usr_logs_size);
				$this->showError($msg, 'notice', $noticeNo);
			}
			if ($this->enable_usr_logs)
			{
				$this->path = LOGS_PATH.$this->usr_logs_name.'.txt';
				$this->type = 'usrN';
				$this->addLogs($msg, 'Notice', $noticeNo);
			}
			return;
		}
		
		Function help($msg, $helpNo=0)
		{
			if ($this->visible_usr_logs)
			{
				if ($this->size == '-')
					$this->setSize($this->usr_logs_size);
				$this->showError($msg, 'help', $helpNo);
			}
			if ($this->enable_usr_logs)
			{
				$this->path = LOGS_PATH.$this->usr_logs_name.'.txt';
				$this->type = 'usrH';
				$this->addLogs($msg, 'Help', $helpNo);
			}
			return;
		}
		
		Function infos($msg, $infoNo=0)
		{
			if ($this->visible_usr_logs)
			{
				if ($this->size == '-')
					$this->setSize($this->usr_logs_size);
				$this->showError($msg, 'infos', $infoNo);
			}
			if ($this->enable_usr_logs)
			{
				$this->path = LOGS_PATH.$this->usr_logs_name.'.txt';
				$this->type = 'usrI';
				$this->addLogs($msg, 'Infos', $infoNo);
			}
			return;
		}
		
		Function warning($msg, $errNo=0)
		{
			if ($this->visible_usr_logs)
			{
				if ($this->size == '-')
					$this->setSize($this->usr_logs_size);
				$this->showError($msg, 'warning', $errNo);
			}
			if ($this->enable_usr_logs)
			{
				$this->path = LOGS_PATH.$this->usr_logs_name.'.txt';
				$this->type = 'usrE';
				$this->addLogs($msg, 'Warning', $errNo);
			}
			$this->eUsrCount++;
			return;
		}
		
	private
		Function showError($msg, $type, $errNo=0)
		{
			if ($errNo == 0)
				echo "<div class=\"$type-$this->size\"><table width=99%><tr><td><span class=\"t-$type-$this->size\">".$msg."</span></td></tr></table></div><br>";
			else
				echo "<div class=\"$type-$this->size\"><table width=99%><tr><td><span class=\"t-$type-$this->size\">".$msg."<br>ErrNo: Epw0x".$errNo."</span></td></tr></table></div><br>";
			$this->setSize();
			return;
		}
		
		Function addLogs($msg, $type, $errNo)
		{
			$date = date('Y-m-d H:i:s');
			
			$log = '['.$date.'] ';
			$log .= '['.$type.'] ';
			if ($this->type == 'sysE')
				$log .= '[ErrorNo. Epw0x'.$errNo.'] ';
			elseif ($this->type == 'sysN')
				$log .= '[NoticeNo. Npw1x'.$errNo.'] ';
			elseif ($this->type == 'sysH')
				$log .= '[HelpNo. Hpw2x'.$errNo.'] ';
			elseif ($this->type == 'sysI')
				$log .= '[InfoNo. Ipw3x'.$errNo.'] ';
			elseif ($this->type == 'usrE')
				$log .= '[ErrorNo. '.$errNo.'] ';
			elseif ($this->type == 'usrN')
				$log .= '[NoticeNo. '.$errNo.'] ';
			elseif ($this->type == 'usrH')
				$log .= '[HelpNo. '.$errNo.'] ';
			elseif ($this->type == 'usrI')
				$log .= '[InfoNo. '.$errNo.'] ';
			$log .= $msg;
			if (trim($this->ePage) != 0)
				$log .= ' in '.$this->ePage;
			if (trim($this->eLine) != 0)
				$log .= ' on line '.$this->eLine;
			$log .= "\n";
			
			if ($fd = fopen($this->path, 'a+'))
			{
				fputs($fd, $log);
				fclose($fd);
			}
			
			$this->ePage = 0;
			$this->eLine = 0;
			return;
		}
}

?>